package com.llx.listener;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.llx.domain.User;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;

import java.io.IOException;

/**
 * @version 1.0
 * @description
 * @author viki
 * @date 2021/8/20 19:22
 */
public class MyMessageListener implements MessageListener {
    // 这个对象的作⽤就是把json的字符串转换为实体类对象
    private ObjectMapper objectMapper = new ObjectMapper();
    @Override
    public void onMessage(Message message) {
        // 1. 获取消息
        byte[] body = message.getBody();
        // 2. 把消息转换为User对象
        try {
            User user = objectMapper.readValue(body, User.class);
            System.out.println("监听器读取到的消息："+ user);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}